package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.watson.R;
import com.watson.utils.StringUtils;

import java.util.ArrayList;
import java.util.Map;


public class QuestAdapter extends RecyclerView.Adapter<QuestAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;

    public QuestAdapter(Context mContext, ArrayList<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_quest, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvBillsource.setText(mList.get(position).get("billsort") + "");
            holder.tverpno.setText(mList.get(position).get("lastbillsort") + "-" + mList.get(position).get("lastbillno"));
            holder.tvkf.setText(StringUtils.isEmpty(mList.get(position).get("kf") + "") ? "" : mList.get(position).get("kf") + "");
            holder.tvmdate.setText(mList.get(position).get("mdate") + "");
            if (!StringUtils.isEmpty(mList.get(position).get("sly") + "")) {
                holder.tvsly.setText(mList.get(position).get("sly") + "" + mList.get(position).get("slyoo"));
                holder.lysly.setVisibility(View.VISIBLE);
            } else {
                holder.lysly.setVisibility(View.GONE);
            }
            holder.tvtaskrecipient.setText(StringUtils.isEmpty(mList.get(position).get("taskrecipient") + "") ? "" : mList.get(position).get("taskrecipient") + "");
            holder.tvout_storage.setText((StringUtils.isEmpty(mList.get(position).get("out_storage") + "") ? "" : mList.get(position).get("out_storage") + "")
                    + "-->" + (StringUtils.isEmpty(mList.get(position).get("in_storage") + "") ? "" : mList.get(position).get("in_storage") + ""));
            String status = mList.get(position).get("status") + "";
            if (StringUtils.isEmpty(status)) {
                status = "";
            }
            //待打印、打包中、待出库、已发货、待收货、已收货
            if (status.equals("待打印") || status.equals("未开始")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status1));
            } else if (status.equals("打包中") || status.equals("未完成")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status2));
            } else if (status.equals("打包完成")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status6));
            } else if (status.equals("待出库")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status3));
            } else if (status.equals("已发货")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status4));
            } else if (status.equals("待收货")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.status5));
            } else if (status.equals("已收货")) {
                holder.tvType.setText(status);
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.tv333));
            } else {
                holder.tvType.setTextColor(mContext.getResources().getColor(R.color.tv666));
                holder.tvType.setText(status);
            }

            holder.tvnum.setText(mList.get(position).get("quantity") + "件");
            String memo = mList.get(position).get("memo") + "";
            if (!StringUtils.isEmpty(memo)) {
                holder.tvmemo.setText(memo + "");
                holder.tvmemo1.setVisibility(View.VISIBLE);
                holder.tvmemo.setVisibility(View.VISIBLE);
            } else {
                holder.tvmemo1.setVisibility(View.GONE);
                holder.tvmemo.setVisibility(View.GONE);
            }

            holder.itemView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvBillsource, tverpno, tvkf, tvsly, tvout_storage, tvmdate, tvmemo, tvnum, tvmemo1, tvType, tvtaskrecipient;
        public LinearLayout lysly;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvBillsource = itemView.findViewById(R.id.tvBillsource);
            tverpno = itemView.findViewById(R.id.tverpno);
            tvkf = itemView.findViewById(R.id.tvkf);
            tvsly = itemView.findViewById(R.id.tvsly);
            tvout_storage = itemView.findViewById(R.id.tvout_storage);
            tvmdate = itemView.findViewById(R.id.tvmdate);
            tvmemo = itemView.findViewById(R.id.tvmemo);
            tvnum = itemView.findViewById(R.id.tvnum);
            tvmemo1 = itemView.findViewById(R.id.tvmemo1);
            tvType = itemView.findViewById(R.id.tvType);
            lysly = itemView.findViewById(R.id.lysly);
            tvtaskrecipient = itemView.findViewById(R.id.tvtaskrecipient);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, ArrayList<Map> mList);
    }
}
